package com.ztesoft.book.api.handler;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ztesoft.book.common.utils.base.SnowFlakeGenerator;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


/**
 * @Description Mybatis Plus 自动填充Handler
 * @Author YangFan
 * @Date 2021-06-24 10:22
 */
@Component
public class MpMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        if (ObjectUtil.isNull(this.getFieldValByName("id", metaObject)) || StrUtil.isEmpty(this.getFieldValByName("id", metaObject).toString())) {
            this.setFieldValByName("id", SnowFlakeGenerator.generateIdStr(), metaObject);
        }
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }


}
